<?php
!defined('M_P') && exit('Forbidden');
define('F_M',true);
InitGP(array('hid','step'),'GP');
$pw_housemapDB = House::loadDB('housemap');
require_once(M_P.'require/field.php');
if (!empty($step) && $step == 'searchinfo') {
	include_once D_P.'data/bbscache/house_config.php';
	/*加载map页面头部*/
	include_once (M_P.'require/core.php');
	switch ($SCR) {
		case 'index' :
			$cateid = 'index';
			$db_tplstyle= $house_indextpl ? $house_indextpl : 'default';
			$csspath	= $house_indexcss ? $house_indextpl : 'default';
			$db_tplpath = $db_mode.'_'.$db_tplstyle.'_';
			break;
		default :
	}
	list($_Navbar,$_LoginInfo) = pwNavBar();
	$jobnum = ($winddb['jobnum']>0) ? "(".$winddb['jobnum'].")" : "";
	require_once PrintEot('housemap_info');
	parsePageContent();
}elseif($step == 'search'){
	define('AJAX',1);
	InitGP(array('type','order'),GP);
	empty($order) && $order = 0;
	$result = array();
	if ($type == 'near') {	//查找周边楼盘
		$results = $pw_housemapDB->searchHouseMapByPosition($hid);
	} elseif ($type == 'history') {
		$houseloginfo  = array();
		if (($houselog = GetCookie('houselog')) && ($houselog = explode(',', trim($houselog, ',')))) {
			$result = $pw_housemapDB->queryHouseMapByHids(array_slice($houselog, -6, 6),$order);
			$results['contents'] = $result;
			$results['type'] = $type;
		}
	} else {
	 	InitGP(array('field','value','page','area','aveprice','status','paystatus','buildtype','opendate','deliverdate'),'GP');
	 	$page < 1 && $page = 1;
	 	$perpage = 15;
	 	$data = array();
	 	/*所属城区*/
	 	if (!empty($area)) {
	 		$data['area'] = $area;
	 	}
	 	/*楼盘均价*/
	 	if (!empty($aveprice)) {
	 		$data['aveprice'] = $aveprice;
	 	}
	 	/*销售状态*/
	 	if (!empty($status)) {
	 		$data['status'] = $status;
	 	}
	 	/*交付状况*/
	 	if (!empty($paystatus)) {
	 		$data['paystatus'] = $paystatus;
	 	}
	 	/*建筑形式*/
	 	if (!empty($buildtype)) {
	 		$data['buildtype'] = $buildtype;
	 	}
	 	/*楼盘类型*/
	 	if (!empty($property)) {
	 		$data['property'] = $property;
	 	}
	 	/*开盘时间*/
	 	if (!empty($opendate)) {
	 		$opendate['start'] = PwStrtoTime($opendate['start']);
	 		$opendate['end'] = PwStrtoTime($opendate['end']);
	 		$data['opendate'] = $opendate;
	 	}
	 	/*入住时间*/
	 	if (!empty($deliverdate)) {
	 		$deliverdate['start'] = PwStrtoTime($deliverdate['start']);
	 		$deliverdate['end'] = PwStrtoTime($deliverdate['end']);
	 		$data['deliverdate'] = $deliverdate;
	 	}
	 	$houseinfo = House::LoadClass('HouseInfo');
	 	$where = $houseinfo->paramCheck($data);
	 	$where['ifopen'] = 1;
	 	$total = $houseinfo->_hdb->getCount($where);
	 	$infodb = $houseinfo->getList($where,($page - 1) * $perpage,$perpage);
	 	$hids = array();
		foreach ($infodb as $key => $value) {
	 		$hids[] = $value['hid'];
	 	}
	 	$result = $pw_housemapDB->queryHouseMapByHids($hids,$order);
	 	$pages = getPagesForMap($page,$total,$perpage);
	 	$results['contents'] = $result;
	 	$results['pages'] = $pages;
	}
	$results = str_replace("\'", "'", pwJsonEncode($results));
	echo $results;
	ajax_footer();exit;
}else{
	//default map info
	$houseinfoDB = House::loadDB('HouseInfo');
	$houseinfo = $houseinfoDB->getListByIds(array($hid));
	if (is_array($houseinfo) && !empty($houseinfo)) {
		$sContent = $houseinfo[0];
		$sTitle = $houseinfo[0]['name'];
	}
	$mapinfo = $pw_housemapDB->queryHouseMapByHid($hid);
	$point_x = $point_y = 0;
	$mapZoom = 12;
	$pointLaber = $pointTitle = '';
	if (is_array($mapinfo) && !empty($mapinfo)) {
		$point_x = $mapinfo['x'];
		$point_y = $mapinfo['y'];
		$mapZoom = $mapinfo['zoom'];
		$pointLaber = $mapinfo['laber'];
		$pointTitle = $mapinfo['name'];
	}
	require_once PrintEot('housemap');
}

function getPagesForMap($cPage,$count,$perpage,$type='f'){
	$cPage = (int)$cPage;
	$cPage < 1 && $cPage = 1;
	$pageCount = ceil($count/$perpage);
	$nextP = $pageCount <= $cPage ? 0 : $cPage + 1;
	$prevP = $cPage <= 1 ? 0 : $cPage - 1;
	$strpages = '<p>';
	$i = 1;
	$nextStr = $prevStr = "";
	while ($i < 4) {
		$nextStr .= ($cPage + $i) <= $pageCount ? '<a href="javascript:;" onclick=setSearchConditions(\'\',\'\',\'\',"'.$type.'",'.(int)($cPage + $i).')>['.(int)($cPage + $i).']</a>' : '';
		$temp_prevStr = ($cPage - $i) > 0 ? '<a href="javascript:;" onclick=setSearchConditions(\'\',\'\',\'\',"'.$type.'",'.(int)($cPage - $i).')>['.(int)($cPage - $i).']</a>' : '';
		$prevStr = $temp_prevStr . $prevStr;
		$i++;
	}
	if ($prevP) {
		$strpages .= '<a class="prev" href="javascript:;" onclick=setSearchConditions(\'\',\'\',\'\',"'.$type.'",'.$prevP.')>下一页</a>';
	}else{
		$strpages .= "<span class='prev'>上一页</span>";
	}
	if ($nextP) {
		$strpages .= '<a class="next" href="javascript:;" onclick=setSearchConditions(\'\',\'\',\'\',"'.$type.'",'.$nextP.')>下一页</a>';
	}else{
		$strpages .= "<span class='next'>下一页</span>";
	}
	$strpages .= $prevStr . '<span class="current">['.$cPage.']</span>' . $nextStr;
	$strpages .= '</p><p>找到条'.$count.'结果,共 <em>'.$pageCount.'</em> 页</p>';
	return $strpages;
}
function parsePageContent(){
	$output = ob_get_contents();
	if ($db_htmifopen) {
		$output = preg_replace(
			"/\<a(\s*[^\>]+\s*)href\=([\"|\']?)((index|cate|thread|read|faq|rss)\.php\?[^\"\'>\s]+\s?)[\"|\']?/ies",
			"Htm_cv('\\3','<a\\1href=\"')",
			$output
		);
	}
	if ($db_redundancy && $SCR!='post') {
		$output = str_replace(
			array("\r",'<!---->-->','<!--<!---->',"<!---->\n",'<!---->','<!-- -->',"<!--\n-->","\t\t",'        ',"\n\t","\n\n"),
			array('','','','','','','','',"\n","\n"),
			$output
		);
	} else {
		$output = str_replace(array('<!---->-->','<!--<!---->',"<!---->\r\n",'<!---->','<!-- -->',"\t\t\t"),'',$output);
	}
	updateCacheData();
	echo ObContents($output);
	unset($output);
	N_flush();
	exit;
}
?>